const app = getApp();
const api = require('../utils/api.js');
let order = {};
order.orderRevoke = function(id,msg,i, order_cancel, callback) {
  let type = Number(order_cancel) === 1 ? 1 : 2;
  app.getSubscribeMessage(type).then(response =>{
  wx.showModal({
    title: '提示',
    content: msg,
    cancelText: '否',
    confirmText: '是',
    success: function(res) {
      if (res.cancel) {
        return true;
      }
      if (res.confirm) {
        wx.showLoading({
          title: '操作中...',
        });
        app.ws_micro_shoprequest({
          url: i == 1 ? api.order.revoke : i == 2 ? api.group.order.revoke:api.miaosha.order_revoke,
          data: {
            order_id: id,
          },
          success: function(res1) {
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: res1.msg,
              showCancel: false,
              success: function(res2) {
                return callback(res2);
              },
            });
          },
        });
      }
    },
  });
  })
}
order.orderConfirm = function(e,i,callback) {
  wx.showModal({
    title: '提示',
    content: '是否确认已收到货？',
    cancelText: '否',
    confirmText: '是',
    success: function(res) {
      if (res.cancel) {
        return true;
      }
      if (res.confirm) {
        wx.showLoading({
          title: '操作中',
        });
        app.ws_micro_shoprequest({
          url: i == 1 ? api.order.confirm :i==2? api.group.order.confirm:api.miaosha.confirm,
          data: {
            order_id: e,
          },
          success: function(res) {
            wx.hideLoading();
            wx.showToast({
              title: res.msg,
            });
            return callback(res);
          },
        });
      }
    },
  });
}
order.orderPay = function(e,id,type) {
  let payment = Number(e.currentTarget.dataset.payment);
  let t = Number(type) === 2 ? 3 : 0;
  app.getSubscribeMessage(t).then(response => {
  wx.showLoading({
    title: '正在提交',
    mask: true,
  });
  var url = type == 1 ? api.order.payDataNew : type == 2 ? api.group.pay_data : api.miaosha.pay_data
  if (payment === 0) {
    WeChatPay(id,url,type);
  } else if (payment === 3) {
    BalancePay(id, url);
  } else if (payment === 5) {
    if (wx.getStorageSync('user_info').unionid && wx.getStorageSync('user_info').card) {
      CardPay(id, url);
    } else {
      wx.hideLoading();
      wx.showModal({
        title: '友情提示',
        content: '你还未开通会员卡，是否前往个人中心开通？',
        success: function (res) {
          if (res.confirm) {
            app.util.navigateTo('/ws_micro_shop/pages/user/user', {}, false);
          }
        },
      });
    }
  }
  })
}
order.deleteOrder = function(e, callback) {
  let {
    orderid: order_id,
    type
  } = e.currentTarget.dataset;
  wx.showModal({
    title: '提示',
    content: '是否要删除该订单',
    success: res => {
      if (res.confirm) {
        app.ws_micro_shoprequest({
          url: api.order.deleteorder,
          data: {
            order_id,
            type: type == 1 ? 'default' : type == 2 ?'pt':'ms',
          },
          success: function(res) {
            if (res.code === 0) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
              });
              return callback(res);
            } else {
              wx.showToast({
                title: res.msg || '删除失败',
                icon: 'none',
              });
            }
          },
        });
      }
    },
  });
}
order.orderQrcode = function (orderNo, orderId,i,callback){
  app.ws_micro_shoprequest({
    url: (i==1 || i==3)?api.order.get_qrcode:api.group.order.get_qrcode,
    data: {
      order_no: orderNo,
    },
    success: function (res) {
      if (res.code === 0) {
        return callback(res)
      } else {
        wx.showModal({
          title: '提示',
          content: res.msg,
        });
      }
    },
    complete: function () {
      wx.hideLoading();
    },
  });
}
order.quickAddCart=function(e){
  let orderId = e.currentTarget.dataset.orderid;
  app.ws_micro_shoprequest({
    url: api.order.againorder,
    data: {
      order_id: orderId,
    },
    success: function (res) {
      if (res.code === 0) {
        wx.showModal({
          title: '提示',
          content: '已添加到购物车了，快去结算吧',
          success: res => {
            if (res.confirm) {
              app.util.navigateTo('/ws_micro_shop/pages/cart/cart');
            }
          },
        });
      }
    },
  });
}
order.ping=function(e){
  let{id,type}=e.currentTarget.dataset;
  app.util.navigateTo('/ws_micro_shop/pages/order-comment/order-comment',{
      id:id,
      entry:type==1?'default':type==2?'pt':'ms'
    }
  )
}
function WeChatPay(id, url, type) {
  app.ws_micro_shoprequest({
    url: url,
    data: {
      order_id: id,
      pay_type: 'WECHAT_PAY',
    },
    complete: function() {
      wx.hideLoading();
    },
    success: function(res) {
      if (res.code === 0) {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          complete: function(e) {
            if (
              e.errMsg === 'requestPayment:fail' ||
              e.errMsg === 'requestPayment:fail cancel'
            ) {
              wx.showModal({
                title: '提示',
                content: '订单尚未支付',
                showCancel: false,
                confirmText: '确认',
                success: function(res) {
                  if (res.confirm) {
                    app.util.navigateTo(
                      '/ws_micro_shop/pages/order/order', {
                        status: 0,
                      },
                      true
                    );
                  }
                },
              });
              return;
            }

            // 普通订单 支付成功之后再调一次接口用来派单 jane 0212
            if (type == 1) {
              app.ws_micro_shoprequest({
                url: api.order.payDataNew,
                data: {
                  order_id: id,
                  pay_type: 'WECHAT_PAY',
                  distributeRider: 1,
                },
                success: function(res) {},
              });
            }
            
            app.util.navigateTo(
              '/ws_micro_shop/pages/order/order', {
                status: 1,
              },
              true
            );
          },
        });
      }
      if (res.code === 1) {
        wx.showToast({
          title: res.msg,
          icon: 'none',
        });
      }
    },
  });
}

function BalancePay(id, url) {
  app.ws_micro_shoprequest({
    url: url,
    data: {
      order_id: id,
      pay_type: 'BALANCE_PAY',
    },
    complete: function() {
      wx.hideLoading();
    },
    success: function(res) {
      if (res.code === 0) {
        app.util.navigateTo(
          '/ws_micro_shop/pages/order/order', {
            status: 1,
          },
          true
        );
      }
      if (res.code === 1) {
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false,
        });
      }
    },
  });
}

function CardPay(id, url) {
  app.ws_micro_shoprequest({
    url: url,
    data: {
      order_id: id,
      pay_type: 'CARD_PAY',
    },
    complete: function() {
      wx.hideLoading();
    },
    success: function(res) {
      if (res.code === 0) {
        app.util.navigateTo(
          '/ws_micro_shop/pages/order/order', {
            status: 1,
          },
          true
        );
      }
      if (res.code === 1) {
        wx.showModal({
          title: '提示',
          content: res.msg,
          showCancel: false,
        });
      }
    },
  });
}
module.exports = order